#!/usr/bin/env python
# -*- coding: utf-8 -*-
from easyexcel import *

def synsheets(transaction_info):

    #sheet1 = "InfoRequest"
    #sheet2 = "ProcessChat"
    sheet1 = 'Sheet1'
    #exfp = easyExcel("D:\Emails\PayPalComplain.xlsx")
    exfp = easyExcel("D:\Emails\PayPal.xlsx")
    search_n = exfp.searchColum(sheet1,1,transaction_info['case_id'])
    try :
        ex_process = transaction_info['process'].decode('utf8').encode('gb2312')
        #excel兼容的编码
    except UnicodeEncodeError :
        ex_process = transaction_info['process']
        
    if search_n == -1 :
        row_n = exfp.find_last_row(sheet1,1)
        print row_n
        #有些里面没有信件里面没有交易的内容，如，case 结束的邮件
        """
        if not transaction_info['invoice_no']:
            exfp.save()
            exfp.close()
            return (0)
        """
        #跟新全套信息（如果有）
        """
        exfp.setCell(sheet1,row_n + 1,2,transaction_info['invoice_no'])
        exfp.setCell(sheet1,row_n + 1,3,transaction_info['case_id'])
        exfp.setCell(sheet1,row_n + 1,4,transaction_info['amount']+' '+transaction_info['currency'])
        exfp.setCell(sheet1,row_n + 1,5,transaction_info['transaction_no'])
        exfp.setCell(sheet1,row_n + 1,6,transaction_info['date'])
        exfp.setCell(sheet1,row_n + 1,7,transaction_info['reason'].decode('utf8').encode('gb2312'))
        exfp.setCell(sheet1,row_n + 1,8,transaction_info['status'].decode('utf8').encode('gb2312'))
        """
        exfp.setCell(sheet1,row_n + 1,1,transaction_info['case_id'])
        exfp.setCell(sheet1,row_n + 1,2,transaction_info['transaction_no'])
        exfp.setCell(sheet1,row_n + 1,3,transaction_info['invoice_no'])
        exfp.setCell(sheet1,row_n + 1,4,transaction_info['reason'].decode('utf8').encode('gb2312'))
        exfp.setCell(sheet1,row_n + 1,5,transaction_info['date'])
        exfp.setCell(sheet1,row_n + 1,9,transaction_info['amount'])
        exfp.setCell(sheet1,row_n + 1,10,transaction_info['currency'])
        #此处可以确认用gb2312编码
        #开始记录投诉过程信息
        exfp.save()
    else :
        """
        投诉有跟新，暂时无法用程序对跟新解码，需人工查看跟新类型，
        在备注里面设置跟新标记，以便人工查看后跟新，并删除标记
        """
        exfp.setCell(sheet1,search_n + 1,14,'Refreshed')
        #曾经有信息，则跟新日期
        #此处应该先比较日期的先后，有时候有些客户会变化投诉信息，这是少数：TODO
        #exfp.setCell(sheet1,search_n + 1,6,transaction_info['date'])
        #查看是否有状态或者原因可以跟新的
        """
        if transaction_info['reason'] :
            exfp.setCell(sheet1,search_n + 1,7,transaction_info['reason'].decode('utf8').encode('gb2312'))
        if transaction_info['status'] :
            exfp.setCell(sheet1,search_n + 1,8,transaction_info['status'].decode('utf8').encode('gb2312'))
        #跟新投诉过程信息
        search_m = exfp.searchColum(sheet2,1,transaction_info['case_id'])
        #print search_m
        if search_m == -1 :
            row_m = exfp.find_last_row(sheet2,1)
            exfp.setCell(sheet2,row_m + 1,1,transaction_info['case_id'])
            exfp.setCell(sheet2,row_m + 1,2,transaction_info['date'])
            exfp.setCell(sheet2,row_m + 1,3,ex_process)
        else :
            col_m = exfp.find_last_colum(sheet2,search_m + 1)
            #print "最后一列：%s"%col_m
            exfp.setCell(sheet2,search_m + 1,col_m + 1,transaction_info['date'])
            exfp.setCell(sheet2,search_m + 1,col_m + 2,ex_process)
        """
        exfp.save()
    exfp.close()
    return (1)
if __name__ == '__main__' :
    info = {'transaction_no': '4EL05309XE4001819',
            'currency': 'USD', 'amount': '57.96',
            'case_id': 'PP-001-414-058-986',
            'date': '2011-8-20',
            'invoice_no': 'en-uk_1106170221_125',
            'process': 'PayPal\xe4\xb9\xb0\xe5\xae\xb6\xe6\x8a\x95\xe8\xaf\x89\xe5\xb7\xb2\xe8\xa7\xa3\xe5\x86\xb3PP-001-380-675-236',
            'status': '争议'
            }
    synsheets(info)
    